<?php
namespace app\wxadmin\controller;

use think\Controller;
use think\Request;
use app\wx\model\Cdb;

class User extends Controller
{
    var $corpid;
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        if(empty(session('qywx_user'))||empty(session('cdb')))
        {
            jump('/wx/login');
        }
    }

    public function index()
    {
        $this->assign('dept_str',model('user')->get_dept_json($this->corpid));

        return $this->fetch('templates/wxadmin/user/index.html');
    }


    function get_user_list()
    {
        $limit=input('get.limit');
        $page=input('get.page');
        $dept_id=input('get.dept_id');
        $offset=$limit*($page-1);
        $db=Cdb::db($this->corpid);
        if($dept_id=='')
        {
            $count_num=$db->name('user')->count();
            $data=$db->name('user')->field('id,avatar,userid,name,gender,position,mobile,status')->order('id asc')->limit($offset,$limit)->select();

        }else{
            //$dept_ids=model('user')->get_dept_ids($dept_id,$this->corpid,true);
            //$dept_ids=implode(',',$dept_ids);
            $count_num=$db->table('__USER__')
                ->alias('u')
                ->join('__USER_DEPT__ d','u.userid = d.userid and d.dept_id in ( '.$dept_id .')')
                ->count();
            $data=$db->table('__USER__')
                ->alias('u')
                ->field('u.id,u.avatar,u.userid,u.name,u.gender,u.position,u.mobile,u.status')
                ->join('__USER_DEPT__ d','u.userid = d.userid and d.dept_id in ( '.$dept_id .')')
                ->order('u.id asc')
                ->limit($offset,$limit)
                ->select();
        }
        if(!empty($data))
        {
            foreach($data as &$v)
            {
                if($v['gender']=='1')
                {
                    $v['gender']='男';
                }else{
                    $v['gender']='女';
                }
                if($v['status']=='0')
                {
                    $v['status']='未激活';
                }elseif($v['status']=='2'){
                    $v['status']='已禁用';
                }else{
                    $v['status']='正常';
                }
                $v['avatar']='<img src="'.$v['avatar'].'" height="40" />';
            }
        }
        if($count_num==0)
        {
            $res['code']=0;
            $res['msg']='暂无任何用户';
            $res['count']=0;
            $res['data']=[];
        }else{
            $res['code']=0;
            $res['msg']='';
            $res['count']=$count_num;
            $res['data']=$data;
        }
        return json($res);
    }
}
?>